from PIL import Image, ImageGrab
import time
import os

class ImageMatch():
    def __init__(self):
        self.screen = ''
        self.screen_data = ''
        self.template = ''
        self.template_data = ''

    def find_image(self, template_name):
        #截取大图
        self.screen = ImageGrab.grab().convert('RGBA')
        self.screen_data = self.screen.load()   #加载大图元数据
        screen_width, screen_height = self.screen.size  #获取大图长宽

        #读取模板图片
        template_path = os.path.abspath('') + '\\template\\' + template_name
        self.template = Image.open(template_path)   #打开模板图片
        template_width, template_height = self.template.size    #获取模板图片长宽
        self.template_data = self.template.load()   #加载模板图片元数据

        pos_x = -1  #初始化坐标, 值为-1, 如果没有匹配到的话, 坐标返回为-1
        pos_y = -1
        #滑动比对
        for x in range(screen_width - template_width):
            for y in range(screen_height - template_height):
                #五个点的匹配
                if (self.compare(self.screen_data[x, y], self.template_data[0, 0])
                    and self.compare(self.screen_data[x + template_width, y], self.template_data[template_width-1, 0])
                    and self.compare(self.screen_data[x, y + template_height], self.template_data[0, template_height-1])
                    and self.compare(self.screen_data[x + template_width, y + template_height], self.template_data[template_width-1, template_height-1])
                    and self.compare(self.screen_data[x + template_width/2, y + template_height/2], self.template_data[template_width/2, template_height/2])):
                    is_match = self.check_match(x, y) #进行全像素匹配
                    if is_match:
                        pos_x = x + int(template_width/2)
                        pos_y = y + int(template_height/2)
                        return pos_x, pos_y
                    # else:
                    #     print(222)
                    #     return pos_x, pos_y

    #全像素匹配
    def check_match(self, x, y, similarity = 1):
        template_width, template_height = self.template.size    #获取模板图片长宽
        total_count = template_width * template_height
        not_match_count = 0
        for w in range(template_width):
            for h in range(template_height):
                if not self.compare(self.screen_data[x + w, y + h], self.template_data[w, h]):
                    not_match_count += 1
        if not_match_count/total_count <= 1 - similarity:
            return True

    def compare(self, d1, d2):
        if d1[0] == d2[0] and d1[1] == d2[1] and d1[2] == d2[2]:
            return True
        else:
            return False
        # return d1[0] == d2[0] and d1[1] == d2[1] and d1[2] == d2[2]

    def check_exsit(self, template_name):
        x, y = self.find_image(template_name)
        if x == -1 and y == -1:
            return False
        else:
            return True

if __name__ == '__main__':
    os.system('start /b calc.exe')
    time.sleep(5)

    match = ImageMatch()
    x, y = match.find_image('2.png')
    print(x, y)

    time.sleep(2)
    os.system('taskkill /f /IM calc.exe')